
var inputData=[];
// 查询筛选条件
function initCharts(data){
    $.ajax({
        url:'/map/searchCommunity',
        data:data,
        success:function(data){
            var html='';
            var obj=data.result;
            // console.log(obj)
            $.each(obj,function(i,obj){
                html+=`
                    <li class="list-group-item">
                        <a href="${obj.communityId}">${obj.communityName}</a>
                        <p class="m-n">数量${obj.count}</p>
                    </li>
                    `
            });

            $('#searchCommunity').html(html);

            // 点击小区进入楼栋
            $('#searchCommunity a').click(function(event){
                event.preventDefault();
                $.ajax({
                    url:'/map/searchCommunityEstateDate',
                    data:{communityId:$(this).attr("href")},
                    success:function(data){
                        var html='';
                        var obj=data.result;
                        $.each(obj,function (i,obj){
                            html+=`
          <li class="list-group-item">
            <a >${obj.sourceAddress}</a>
            <ul class='p-n' style="list-style: none">
                <li>
                    成交总价：<span></span>元 | 贷款金额：<span>${obj.loansAmount}</span>元
                </li>
                <li>
                    放款日期：<span>${obj.startDate}</span> | 贷款期数：<span>${obj.loansDuration}</span>期
                </li>
                <li>
                    当前评估价：<span>${obj.loansEvaluateTotalPrice}</span>元 | 动态抵押率：<span>${obj.cltv}</span>
                </li>
            </ul>
        </li>
                `
                        });
                        $('#searchCommunity').html(html);

                    }
                })
                // 地图
                var cluster = "/Map/clusterDate",//行政区数据接口
                    Community = " /map/searchCommunity",//小区数据接口
                    searchEstate = "/map/searchEstateDate",//楼栋数据接口
                    allAreaResult;
                //请求接口
                function clusterDate (url,zoneNumber) {
                    $.ajax({
                        type: "post",
                        url: url,
                        data:zoneNumber,
                        async: false,
                        success:function (data) {
                            allAreaResult = data.result;
                        }
                    })
                };
                clusterDate(Community);
                function createCluster (data) {
                    data.forEach(function (data) {
                        //console.log(data);
                        //自定义覆盖物
                        function ComplexCustomOverlay(point, text,num){
                            this._point = point;
                            this._text = text;
                            this._num = num;
                        }
                        ComplexCustomOverlay.prototype = new BMap.Overlay();
                        ComplexCustomOverlay.prototype.initialize = function(map){
                            this._map = map;

                            map.clearOverlays();
                            //map.removeOverlay();
                            map.centerAndZoom(new BMap.Point(this.lng, this.lat),16);
                            var zoneNumber = {zoneNumber:data.zoneNumber};
                            clusterDate(Community,zoneNumber);
                            allAreaResult.forEach(function (Community) {
                                //自定义小区覆盖物
                                //console.log(Community);
                                function ComplexCustomOverlay(point, text,num){
                                    this._point = point;
                                    this._text = text;
                                    this._num = num;
                                }
                                ComplexCustomOverlay.prototype = new BMap.Overlay();
                                ComplexCustomOverlay.prototype.initialize = function(map){
                                    this._map = map;

                                        map.clearOverlays();
                                        //map.removeOverlay();
                                        map.centerAndZoom(new BMap.Point(Community.lng, Community.lat),18);
                                        var communityId = {communityId:Community.communityid};
                                        clusterDate(searchEstate,communityId);
                                        var markers = [];
                                        var pt = null;
                                        var i = 0;
                                        for (; i < allAreaResult.length; i++) {
                                            pt = new BMap.Point(allAreaResult[i].lng, allAreaResult[i].lat);
                                            markers.push(new BMap.Marker(pt));
                                        }
                                        //最简单的用法，生成一个marker数组，然后调用markerClusterer类即可。
                                        var markerClusterer = new BMapLib.MarkerClusterer(map, {markers:markers});


                                    map.getPanes().labelPane.appendChild(div);
                                    return div;
                                };
                                ComplexCustomOverlay.prototype.draw = function(){
                                    var map = this._map;
                                    var pixel = map.pointToOverlayPixel(this._point);
                                    this._div.style.left = pixel.x - parseInt(this._arrow.style.left) + "px";
                                    this._div.style.top  = pixel.y - 30 + "px";
                                };
                                var pt = new BMap.Point(Community.lng,Community.lat);
                                var count = Community.count + "笔";
                                myCompOverlay = new ComplexCustomOverlay(pt, Community.communityname, count);
                                map.addOverlay(myCompOverlay);
                            });


                            map.getPanes().labelPane.appendChild(div);

                            return div;
                        };
                        ComplexCustomOverlay.prototype.draw = function(){
                            var map = this._map;
                            var pixel = map.pointToOverlayPixel(this._point);
                            this._div.style.left = pixel.x - parseInt(this._arrow.style.left) + "px";
                            this._div.style.top  = pixel.y - 30 + "px";
                        };
                        var pt = new BMap.Point(data.lng,data.lat);
                        var count = data.count + "笔";
                        myCompOverlay = new ComplexCustomOverlay(pt, data.name, count);
                        map.addOverlay(myCompOverlay);
                    })
                }
                createCluster(allAreaResult);
                // 地图

            })

        }
    })
}

$('#searchCommunity').css('display','none');
search.onkeyup=function(){
    $('#searchCommunity').css('display','block');
    var inputKW=this.value;
    if(!inputKW){
        $('#searchCommunity').css('display','none');
    }
    inputData={communityName:inputKW};
    initCharts(inputData)
    // 地图

}


$('#searchData').click(function(){
    var communityName=$("#search").val(),
        city=$('#city').val(),
        district=$('#district').val(),
        startDateStarts = $('#startDateStart').val(),
        startDateEnds = $('#startDateEnd').val(),
        buildArea=$('#buildArea').val(),
        loansDuration=$('#loansDuration').val(),
        inputData={
            communityName:communityName,
            city:city,
            district:district,
            startDateStarts:startDateStarts,
            startDateEnds:startDateEnds,
            buildArea:buildArea,
            loansDuration:loansDuration
        };
    initCharts(inputData)
    $('#searchCommunity').css('display','block');
})